<template>
    <div class="light-Ct">
        <div class="item-Cd item-Cd-Dom1"
            :class="{ 'item-Cd-Dom1-line': parentChildrenLength && parentChildrenLength > 1 || (parentChildrenLength && parentChildrenLength > 2 && item.children && item.children.length > 2) }">

            <el-popover placement="top" :width="200" trigger="hover">
                <template #reference>
                    <div class="item-name">
                        <div v-if="!isEditing" @click="startEdit" class="pointers">{{ item.title }}</div>
                        <el-input v-else v-model="editedName" @blur="finishEdit" @keyup.enter="finishEdit"
                            ref="editInput" style="width: 100%;" />
                    </div>
                </template>
                <div class="operation">
                    <el-button link type="primary" @click="addBrotherItem(index, item)">同级 +</el-button>
                    <el-divider direction="vertical" />
                    <el-button link type="primary" @click="addChildItem(index, item)">子级 +</el-button>
                    <el-divider direction="vertical" />
                    <el-button link type="danger" @click="deleteItem(index, item)">删除</el-button>
                    <el-divider v-if="item.children && item.children.length" direction="vertical" />
                    <el-button v-if="item.children && item.children.length" link type="primary"
                        @click="toggleExpand(index, item)">
                        {{ item.expanded ? '收起' : '展开' }}
                    </el-button>
                </div>
            </el-popover>

        </div>
        <div v-if="item.expanded && item.children && item.children.length"
            :class="item.children && item.children.length ? 'childItem childItemSon' : 'childItem'">
            <RecursiveItemTB v-for="(child, childIndex) in item.children" :key="childIndex" :item="child"
                :index="childIndex" @editItem="editItem" @addBrotherItem="addBrotherItem" @addChildItem="addChildItem"
                @deleteItem="deleteItem" :parentChildrenLength="item.children.length" @toggleExpand="toggleExpand" />
        </div>
    </div>
</template>

<script setup>
import { ref, defineProps, defineEmits, nextTick } from 'vue';
import RecursiveItemTB from './RecursiveItemTB.vue'; // 确保路径正确

const props = defineProps({
    item: {
        type: Object,
        required: true
    },
    index: {
        type: Number,
        required: true
    },
    parentChildrenLength: {
        type: Number,
        default: 0
    }
});

const emit = defineEmits(['editItem', 'addBrotherItem', 'addChildItem', 'deleteItem', 'toggleExpand']);

const isEditing = ref(false);
const editedName = ref(props.item.name);
const editInput = ref(null);

const startEdit = () => {
    isEditing.value = true;
    nextTick(() => {
        editInput.value.focus();
    });
}

const finishEdit = () => {
    isEditing.value = false;
    if (editedName.value !== props.item.name) {
        emit('editItem', props.index, { ...props.item, name: editedName.value });
    }
}

const addBrotherItem = (index, item) => {
    emit('addBrotherItem', index, item);
}

const addChildItem = (index, item) => {
    emit('addChildItem', index, item);
}

const deleteItem = (index, item) => {
    emit('deleteItem', index, item);
}

const toggleExpand = (index, item) => {
    emit('toggleExpand', index, item);
}
</script>

<style lang="scss" scoped>
.light-item {

    &.light-itemTop,
    &.light-itemBottom {
        .light-Ct {
            .item-Cd {
                // padding: 20px 5px;
                box-sizing: border-box;
                position: relative;
                min-width: 76px;
                font-size: var(--global-font-size);
                display: flex;
                align-items: center;
                justify-content: center;

                .item-name {
                    color: var(--global-font-color);
                    border: 1px solid #ddd;
                    border-radius: 5px;
                    background-color: #f9f9f9;
                    transition: background-color 0.3s;
                    width: auto;
                    display: inline-block;
                    // margin-top: 8px;
                    font-size: 12px;
                    padding: 4px 8px;
                    min-width: 24px;
                    text-align: center;

                }
            }
        }

        .childItem {
            position: relative;
            display: flex;
            justify-content: center;
            margin-top: 24px;

            .item-Cd-Dom1::before {
                content: "";
                position: absolute;
                left: 50%;
                top: -24px;
                width: 1px;
                height: 24px;
                background-color: rgb(43, 38, 38);
            }

            .item-Cd-Dom1-line::after {
                content: "";
                position: absolute;
                left: 0px;
                top: -24px;
                width: 100%;
                height: 1px;
                background-color: rgb(43, 38, 38);
            }

            >:first-child>.item-Cd-Dom1-line::after {
                content: "";
                position: absolute;
                left: 50%;
                top: -24px;
                width: 50%;
                height: 1px;
                background-color: black;
            }

            >:last-child>.item-Cd-Dom1-line::after {
                content: "";
                position: absolute;
                left: 0px !important;
                top: -24px;
                width: 50%;
                height: 1px;
                background-color: black;
            }

        }

        .childItemSon {
            padding-top: 24px;

            >.light-Ct::before {
                content: "";
                position: absolute;
                left: 50%;
                top: -24px;
                width: 1px;
                height: 24px;
                background-color: black;
            }
        }


        .pointers {
            cursor: pointer;
        }

    }

    &.light-itemBottom {
        transform: scaleY(-1);

        .pointers {
            transform: scaleY(-1);
        }
    }
}
</style>